package com.ikas.ai.server.common.parser;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.ikas.ai.framework.node.NodeType;
import com.ikas.ai.server.common.parser.model.AiMaintainMetaData;
import com.ikas.ai.server.common.parser.model.FlowNodeLineMetaLine;
import com.ikas.ai.server.common.parser.model.FlowNodeMetaData;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @Description: 评分画布解析
 *
 * @Author: qi.yilai
 * @Date: 2023年2月1日11:21:01
 * @Version: 1.0.0
 **/
@Slf4j
public class AiMaintainParser {
    private static final String NODES = "nodes";
    private static final String EDGES = "edges";

    private static final String DATA = "data";
    private static final String ID = "id";
    private static final String NODE_TYPE_ID = "nodeTypeId";
    private static final String NODE_TYPE_NAME = "nodeTypeName";
    private static final String NAME = "name";
    private static final String VALUE = "value";

    private static final String SOURCE = "source";
    private static final String TARGET = "target";
    private static final String CELL = "cell";

    /**
     * 解析画布
     *
     * @param content
     * @return
     */
    public static AiMaintainMetaData parse(String content) {
        JSONObject root = JSON.parseObject(content);
        JSONArray nodes = root.getJSONArray(NODES);
        JSONArray edges = root.getJSONArray(EDGES);

        AiMaintainMetaData aiMaintainMetaData = AiMaintainMetaData.builder().build();

        for (int i = 0; i < nodes.size(); i++) {
            JSONObject node = nodes.getJSONObject(i);
            aiMaintainMetaData.putFlowNode(parseNode(node));
        }

        List<FlowNodeLineMetaLine> lineMetaDataList = Lists.newArrayList();
        for (int i = 0; i < edges.size(); i++) {
            JSONObject edge = edges.getJSONObject(i);
            lineMetaDataList.add(parseEdge(edge));
        }
        aiMaintainMetaData.setLines(lineMetaDataList);
        return aiMaintainMetaData;
    }

    /**
     * 解析算法节点
     *
     * @param node
     */
    private static FlowNodeMetaData parseNode(JSONObject node) {
        // 读取节点类型、名称
        JSONObject data = node.getJSONObject(DATA);
        String nodeId = node.getString(ID);
        Integer nodeTypeId = data.getInteger(NODE_TYPE_ID);
        String nodeTypeName = data.getString(NODE_TYPE_NAME);
        String name = data.getString(NAME);
        Object value = data.get(VALUE);

        FlowNodeMetaData flowNodeMetaData = FlowNodeMetaData.builder().id(nodeId).name(name).nodeType(nodeTypeId)
                .nodeTypeName(nodeTypeName).value(value.toString()).build();
        return flowNodeMetaData;
    }

    /**
     * 解析 连线
     *
     * @param edge
     */
    private static FlowNodeLineMetaLine parseEdge(JSONObject edge) {
        JSONObject source = edge.getJSONObject(SOURCE);
        String fromNodeId = source.getString(CELL);
        JSONObject target = edge.getJSONObject(TARGET);
        String toNodeId = target.getString(CELL);
        return FlowNodeLineMetaLine.builder().formNodeId(fromNodeId).toNodeId(toNodeId).build();
    }
}
